/******************************************
 * Author: Dave Spadea, Spadea Enterprises, Inc.
 * Date: November 2010
 * 
 * This code is released by Spadea Enterprises, Inc for general consumption 
 * under the Apache Software License.
 * 
 */


package net.spadea.framework.messaging;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

public class BrokerPublisher extends BrokerConnection {

	Connection conn;
	
	public BrokerPublisher(String broker_url) {
		conn = getConnection(broker_url);
	}
	
	public void send(QueueChannel destQueue, String message)
	{
		try {
			Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = sess.createQueue(destQueue.getName());
			MessageProducer producer = sess.createProducer(queue);
			TextMessage tm = sess.createTextMessage(message);
			producer.send(tm);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public void send(TopicChannel destTopic, String message) 
	{
		try {
			Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = sess.createTopic(destTopic.getName());
			MessageProducer producer = sess.createProducer(topic);
			TextMessage tm = sess.createTextMessage(message);
			producer.send(tm);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void send(Channel channel, String message)
	{
		if (channel.getType().equals("TOPIC")) 
			send((TopicChannel) channel, message);
		else if (channel.getType().equals("QUEUE"))
			send((QueueChannel) channel, message);		
	}
	
}
